home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch5 / PrvPages.frm (.txt) < prev    next >
Visual Basic Form  |  1999-04-17  |  8KB  |  229 lines

  1. VERSION 5.00
  2. Begin VB.Form frmPrvPages 
  3.    Caption         =   "PrvPages"
  4.    ClientHeight    =   4230
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   6270
  8.    BeginProperty Font 
  9.       Name            =   "MS Sans Serif"
  10.       Size            =   8.25
  11.       Charset         =   0
  12.       Weight          =   700
  13.       Underline       =   0   'False
  14.       Italic          =   0   'False
  15.       Strikethrough   =   0   'False
  16.    EndProperty
  17.    LinkTopic       =   "Form1"
  18.    ScaleHeight     =   4230
  19.    ScaleWidth      =   6270
  20.    StartUpPosition =   3  'Windows Default
  21.    Begin VB.CommandButton cmdPreview 
  22.       Caption         =   "Preview"
  23.       Height          =   495
  24.       Left            =   1440
  25.       TabIndex        =   4
  26.       Top             =   2880
  27.       Width           =   1215
  28.    End
  29.    Begin VB.TextBox txtPage 
  30.       Height          =   285
  31.       Left            =   720
  32.       TabIndex        =   3
  33.       Text            =   "1"
  34.       Top             =   3000
  35.       Width           =   495
  36.    End
  37.    Begin VB.CommandButton cmdPrint 
  38.       Caption         =   "Print"
  39.       Height          =   495
  40.       Left            =   2880
  41.       TabIndex        =   1
  42.       Top             =   2880
  43.       Width           =   1215
  44.    End
  45.    Begin VB.TextBox txtLongText 
  46.       Height          =   2655
  47.       Left            =   120
  48.       MultiLine       =   -1  'True
  49.       ScrollBars      =   2  'Vertical
  50.       TabIndex        =   0
  51.       Top             =   120
  52.       Width           =   4455
  53.    End
  54.    Begin VB.Label Label1 
  55.       Caption         =   "Page"
  56.       Height          =   255
  57.       Left            =   120
  58.       TabIndex        =   2
  59.       Top             =   3000
  60.       Width           =   495
  61.    End
  62. Attribute VB_Name = "frmPrvPages"
  63. Attribute VB_GlobalNameSpace = False
  64. Attribute VB_Creatable = False
  65. Attribute VB_PredeclaredId = True
  66. Attribute VB_Exposed = False
  67. Option Explicit
  68. ' Print a string on a Printer or PictureBox,
  69. ' wrapped within the margins.
  70. Private Sub PrintWrappedText(ByVal ptr As Object, ByVal target_page As Integer, ByVal txt As String, ByVal indent As Single, ByVal left_margin As Single, ByVal top_margin As Single, ByVal right_margin As Single, ByVal bottom_margin As Single)
  71. Dim next_paragraph As String
  72. Dim next_word As String
  73. Dim pos As Integer
  74. Dim current_page As Integer
  75.     ' Start at the top of the page.
  76.     Printer.CurrentY = top_margin
  77.     ' Keep track of the page we are on.
  78.     current_page = 1
  79.     ' Repeat until the text is all printed.
  80.     Do While Len(txt) > 0
  81.         ' Get the next paragraph.
  82.         pos = InStr(txt, vbCrLf)
  83.         If pos = 0 Then
  84.             ' Use the rest of the text.
  85.             next_paragraph = Trim$(txt)
  86.             txt = ""
  87.         Else
  88.             ' Get the paragraph.
  89.             next_paragraph = Trim$(Left$(txt, pos - 1))
  90.             txt = Mid$(txt, pos + Len(vbCrLf))
  91.         End If
  92.         ' Indent the paragraph.
  93.         Printer.CurrentX = left_margin + indent
  94.         ' Print the paragraph.
  95.         Do While Len(next_paragraph) > 0
  96.             ' Get the next word.
  97.             pos = InStr(next_paragraph, " ")
  98.             If pos = 0 Then
  99.                 ' Use the rest of the paragraph.
  100.                 next_word = next_paragraph
  101.                 next_paragraph = ""
  102.             Else
  103.                 ' Get the word.
  104.                 next_word = Left$(next_paragraph, pos - 1)
  105.                 next_paragraph = Trim$(Mid$(next_paragraph, pos + 1))
  106.             End If
  107.             ' See if there is room for this word.
  108.             If Printer.CurrentX + Printer.TextWidth(next_word) _
  109.                 > right_margin _
  110.             Then
  111.                 ' It won't fit. Start a new line.
  112.                 Printer.Print
  113.                 Printer.CurrentX = left_margin
  114.                 ' See if we have room for a new line.
  115.                 If Printer.CurrentY + Printer.TextHeight(next_word) _
  116.                     > bottom_margin _
  117.                 Then
  118.                     ' Start a new page.
  119.                     current_page = current_page + 1
  120.                     If current_page > target_page Then Exit Sub
  121.                     Printer.CurrentX = left_margin
  122.                     Printer.CurrentY = top_margin
  123.                 End If
  124.             End If
  125.             ' Now print the word. The ; makes the
  126.             ' Printer not move to the next line.
  127.             If current_page = target_page Then
  128.                 ' Print the word.
  129.                 If ptr Is Printer Then
  130.                     ' This is the printer. Print.
  131.                     ptr.Print next_word & " ";
  132.                 Else
  133.                     ' This is not the printer. Go to
  134.                     ' the printer's position and print.
  135.                     ptr.CurrentX = Printer.CurrentX
  136.                     ptr.CurrentY = Printer.CurrentY
  137.                     ptr.Print next_word & " ";
  138.                     ' Skip space for the word.
  139.                     Printer.CurrentX = Printer.CurrentX + _
  140.                         Printer.TextWidth(next_word & " ")
  141.                 End If
  142.             Else
  143.                 ' Skip space for the word.
  144.                 Printer.CurrentX = Printer.CurrentX + _
  145.                     Printer.TextWidth(next_word & " ")
  146.             End If
  147.         Loop
  148.         ' Finish the paragraph by ending the line.
  149.         Printer.Print
  150.     Loop
  151.     ' See if we got to the desired page yet.
  152.     If current_page < target_page Then
  153.         MsgBox "This page does not exist."
  154.     End If
  155. End Sub
  156. ' Preview the text.
  157. Private Sub cmdPreview_Click()
  158.     MousePointer = vbHourglass
  159.     ' Select a big font so we have more than one page.
  160.     Printer.Font.Name = "Arial"
  161.     Printer.Font.Size = 20
  162.     ' Make the preview PictureBox use the same font.
  163.     With frmPreview.picInner.Font
  164.         .Name = Printer.Font.Name
  165.         .Bold = Printer.Font.Bold
  166.         .Italic = Printer.Font.Italic
  167.         .Strikethrough = Printer.Font.Strikethrough
  168.         .Underline = Printer.Font.Underline
  169.         .Size = Printer.Font.Size
  170.     End With
  171.     frmPreview.picInner.ScaleMode = Printer.ScaleMode
  172.     frmPreview.picInner.Move 0, 0, Printer.ScaleWidth, Printer.ScaleHeight
  173.     ' Print the text.
  174.     PrintWrappedText _
  175.         frmPreview.picInner, CInt(txtPage.Text), _
  176.         txtLongText.Text, 720, 1440, 1440, _
  177.         Printer.ScaleWidth - 1440, _
  178.         Printer.ScaleHeight - 1440
  179.     Printer.KillDoc
  180.     ' Make the preview form tell what page it shows.
  181.     frmPreview.Caption = "Preview Page " & Format$(txtPage.Text)
  182.     ' Display the preview.
  183.     frmPreview.Show vbModal
  184.     MousePointer = vbDefault
  185. End Sub
  186. ' Print the text.
  187. Private Sub cmdPrint_Click()
  188.     Screen.MousePointer = vbHourglass
  189.     ' Select a big font so we have more than one page.
  190.     Printer.Font.Name = "Arial"
  191.     Printer.Font.Size = 20
  192.     ' Print the text.
  193.     PrintWrappedText _
  194.         Printer, CInt(txtPage.Text), _
  195.         txtLongText.Text, 720, 1440, 1440, _
  196.         Printer.ScaleWidth - 1440, _
  197.         Printer.ScaleHeight - 1440
  198.     Printer.EndDoc
  199.     Screen.MousePointer = vbDefault
  200. End Sub
  201. Private Sub Form_Load()
  202. Dim fname As String
  203. Dim fnum As Integer
  204. Dim txt As String
  205.     fname = App.Path
  206.     If Right$(fname, 1) <> "\" Then fname = fname & "\"
  207.     fname = fname & "longtext.txt"
  208.     On Error Resume Next
  209.     fnum = FreeFile
  210.     Open fname For Input As fnum
  211.     txt = Input$(LOF(fnum), fnum)
  212.     Close fnum
  213.     txtLongText.Text = txt
  214. End Sub
  215. ' Make the TextBox as big as possible.
  216. Private Sub Form_Resize()
  217. Dim wid As Single
  218. Dim hgt As Single
  219.     wid = ScaleWidth - 2 * 120
  220.     If wid < 120 Then wid = 120
  221.     hgt = ScaleHeight - cmdPrint.Height - 3 * 120
  222.     If hgt < 120 Then hgt = 120
  223.     txtLongText.Move 120, 120, wid, hgt
  224.     cmdPreview.Top = ScaleHeight - cmdPrint.Height - 120
  225.     cmdPrint.Top = cmdPreview.Top
  226.     Label1.Top = cmdPreview.Top
  227.     txtPage.Top = cmdPreview.Top
  228. End Sub
  229.